package com.webside.subject.service.impl;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.webside.base.baseservice.impl.AbstractService;
import com.webside.series.model.Bdictionary;
import com.webside.subject.mapper.SubjectMapper;
import com.webside.subject.mapper.SubjectSeriesMapper;
import com.webside.subject.model.SubjectEntity;
import com.webside.subject.service.SubjectService;
import com.webside.util.PageUtil;
@Service("SubjectService")
public class SubjectServiceImpl extends AbstractService<SubjectEntity, Long> implements SubjectService {
	@Autowired
	private SubjectMapper subjectMapper;
	@Autowired
	private SubjectSeriesMapper subjectSeriesMapper;
	//这句必须要加上。不然会报空指针异常，因为在实际调用的时候不是BaseMapper调用，而是具体的mapper，这里为userMapper
	@Autowired
	public void setBaseMapper() {
		super.setBaseMapper(subjectMapper);
	}
	@Override
	public List<SubjectEntity> queryAllSubject() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int deleteSubject(List<String> subjectId) {
		// TODO Auto-generated method stub
		return subjectMapper.deleteSubject(subjectId);
	}

	@Override
	public List<String> selectSubjectByName() {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public List<SubjectEntity> queryListByPage(Map<String, Object> parameter) {
		// TODO Auto-generated method stub
		return super.queryListByPage(parameter);
	}
	@Override
	public int updateSubject(SubjectEntity subject) {
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");// 可以方便地修改日期格式
		String nowTime = dateFormat.format(now);
		ParsePosition pos = new ParsePosition(0);
		  Date currentTime_2 = dateFormat.parse(nowTime, pos);
		 subject.setUpdateTime(currentTime_2);
		 int count =0;
		 count = subjectSeriesMapper.queryCountBySubjectId(subject.getId());
		 subject.setSeriesCount(count);
		 subjectMapper.update(subject);
		 int tag = 0;
		 if(subjectMapper.selectSubject(subject)!=null){
			 tag = 1;
		 }
		return tag;
	}
	@Override
	public int addSubject(SubjectEntity subject) {
		/*Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");// 可以方便地修改日期格式
		String nowTime = dateFormat.format(now);
		ParsePosition pos = new ParsePosition(8);
		  Date currentTime_2 = dateFormat.parse(nowTime, pos);
		subject.setCreateTime(currentTime_2);*/
		 int count =0;
		/* count = subjectSeriesMapper.queryCountBySubjectId(subject.getSubjectId());*/
		 subject.setSeriesCount(count);
		 subject.setDeleteState(0);
		return subjectMapper.insert(subject);
	}
	@Override
	public List<HashMap<String, Object>> querySeries(PageUtil pageUtil, String sort, String code) {
		// TODO Auto-generated method stub
		return subjectMapper.querySeries(pageUtil, sort, code);
	}
	@Override
	public int updateAddSubject(SubjectEntity subject) {
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");// 可以方便地修改日期格式
		String nowTime = dateFormat.format(now);
		ParsePosition pos = new ParsePosition(0);
		  Date currentTime_2 = dateFormat.parse(nowTime, pos);
		subject.setCreateTime(currentTime_2);
		 int count =0;
		 count = subjectSeriesMapper.queryCountBySubjectId(subject.getId());
		 subject.setSeriesCount(count);
		 subject.setDeleteState(1);
		 subjectMapper.update(subject);
		 int tag = 0;
		 if(subjectMapper.selectSubject(subject)!=null){
			 tag = 1;
		 }
		return tag;
	}
	@Override
	public SubjectEntity selectSubjectBySubject(SubjectEntity subject) {
		// TODO Auto-generated method stub
		return subjectMapper.findBySubject(subject);
	}
	@Override
	public List<SubjectEntity> selectSubjectByMap(Map<String, Object> parameters) {
		// TODO Auto-generated method stub
		return subjectMapper.selectSubjectByMap(parameters);
	}
	//修改.Map
	@Override
	public void updateSubjectByMap(Map<String, Object> parameters) {
		subjectMapper.updateSubjectByMap(parameters);
		
	}

}
